import React from 'react';
import {Button, Form, Table, Tag,} from 'antd';
import API  from '../../api/api'
import {Link} from "react-router-dom";
import IconFont6 from "../../util/autoIcon/folder";
import ModuleManage from "../../components/ModulePage/ModuleManage";
import judgeFileType from "../../service/judgeFileType";
import To403 from '../StatusCode/403'
import To404 from "../StatusCode/404";
import To500 from "../StatusCode/500";


let config = require('../../util/config').config

class FilePage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data:[]
        }
        this.storeFresh(this.props.data,this.props.type);
    }

    columns = [
        {
            title: '名称',
            dataIndex: 'name',
            fixed: 'left',
            ellipsis:true,
            width:400,
            key: 'name',
            render: (text,record) => {
                let type;
                // 1、判断 record.type是否为 config.folder_d 、config.file_d 或者 config.folder
                if(record.type == config.folder){
                    return <Link to={`/eip/table/folder&${record.key}`}><IconFont6 type="icon-huabanfuben"/>&nbsp;&nbsp;{text}</Link>
                }else {
                    if(record.type==config.article){
                        type = "article"
                        return <Link to={`/eip/file/${type}/${record.key}`}>{text}</Link>
                    }else {
                        type = "file"
                        return <a onClick={_=>{API.downloadFile(record.key)}}>{judgeFileType(text)}&nbsp;&nbsp;{text}</a>
                    }
                }
            }
        },
        {
            title: '类型',
            dataIndex: 'type',
            key: 'type',
            ellipsis:true,
            width:100,
        },
        {
            title: "建立人",
            dataIndex: 'createUser',
            key: 'createUser',
            ellipsis:true,
            width:230,
        },
        {
            title: "建立时间",
            dataIndex: 'createTime',
            key: 'createTime',
            ellipsis:true,
            width:230,
        },
        {
            title: "修改人",
            dataIndex: 'updateUser',
            key: 'updateUser',
            ellipsis:true,
            width:230,
        },
        {
            title: "修改时间",
            dataIndex: 'updateTime',
            key: 'updateTime',
            ellipsis:true,
            width:190,
        },
        {
            title: "状态",
            dataIndex: 'status',
            key: 'status',
            ellipsis:true,
            width:150,
            fixed: 'right',
            render:(text,record) => {
                return (
                    <span>
                        {this.isTop(text)}
                        {this.isNotify(record)}
                    </span>
                )
            }
        }
    ];

    data = []

    permission = false

    result = null;

    isTop = (text)=>{
        if(text=="Y"){
            return <Tag color="#f50">{config.top_status}</Tag>
        }
    }

    isNotify = (record)=>{
        let currentDate = new Date()
        let date = new Date(record.updateTime);
        if(record.new!="" && record.new!=null){
            date.setDate(date.getDate()+Number.parseInt(record.new));
            if(currentDate<=date){
                return <Tag color="#87d068">{config.new_notify}</Tag>
            }
        }
    }

    UNSAFE_componentWillReceiveProps(nextProps, nextContext) {
        this.storeFresh(nextProps.data,nextProps.type);
    }

    storeFresh=(id,type)=>{

        this.result = API.getFiles(id,type);

        let modules
        let module
        let title

        if(this.result.getFile=="no permission"){
            this.permission = false
        }
        else {
            if(type!="folder"){
                modules =JSON.parse(sessionStorage.getItem("getModule"))
                module = modules.filter(item=>item.moduleType==type&&item.moduleId==id)
                title = module[0].moduleName
            }

            let item = {title:title,type:type,id:id};
            API.userAccessLog(item);
            sessionStorage.setItem("getFiles",JSON.stringify(this.result));

            let tmpData = this.result.getFile
            this.pushFileData(tmpData)

            this.permission = true
        }

    }

    pushFileData=(tmpData)=>{
        if(tmpData == []){
            return
        }
        let localData = [];
        for (let i = 0; i <tmpData.length ; i++) {
            let node = tmpData[i];
            let id = node.fileId;
            let name = node.fileName;
            let status = node.status;
            let createUser = node.createUser;
            let createTime = node.createTime;
            let updateUser = node.updateUser;
            let updateTime = node.updateTime;
            let type;
            if(node.fileType == "file"){
                type = "文件"
            }
            else if(node.fileType=="article"){
                type = "文章"
            }else {
                type = "文件夹"
            }
            localData.push({
                new: node.new,
                key: id,
                name: name,
                type: type,
                status:status,
                createUser: createUser,
                createTime: createTime,
                updateUser:updateUser,
                updateTime:updateTime
            });
        }
        this.data = localData;
    }

    changeSubscribeStatus(value){
        let type = value.type
        let id = value.data
        let status
        if(sessionStorage.getItem("subStatus")=="true"){
            status = API.cancelSub({type:type,id:id})
        }else if(sessionStorage.getItem("subStatus")=="false"){
            status = API.submitSub({type:type,id:id})
        }
        if(status=="ok"){
            this.setState({})
        }
    }

    render(){
        let subStatus
        let type
        let params
        if(this.permission){
            if(this.result.manageFlag != "true"){
                subStatus = API.subStatus({type:this.props.type,id:this.props.data});
                sessionStorage.setItem("subStatus",subStatus)
            }

            if(this.props.type=="file-d"){
                type="file"
            }else if(this.props.type=="article-d"){
                type="article"
            }else if(this.props.type=="folder"){
                type="folder-f"
            }
            params = {
                params:{
                    id:type+"&"+this.props.data
                }
            }
        }

        return (
            this.permission == true ?
                this.result.manageFlag == "true"
                    ?
                    <ModuleManage  linkType="userPage" match={params} manageFlag="true"></ModuleManage>
                    :
                    <>
                        <Form layout={"inline"}
                        >
                            <Form.Item
                                name="subscribe"
                                label="訂閱管理"
                                style={{display:"inline"}}
                            >
                                <Button style={{marginBottom:"1rem"}} type={subStatus=="true"?"danger":"primary"} onClick={()=>{this.changeSubscribeStatus(this.props)}}>
                                    {subStatus=="true"?"取消訂閱":"訂閱"}
                                </Button>
                            </Form.Item>
                        </Form>
                        <Table columns={this.columns} dataSource={this.data} scroll={{ x: 1500 }}/>
                    </>
                :
                <To403></To403>
        )
    }
}

export default FilePage
